<%@ page import="es.onlysolutions.gs.web.controller.VideoController" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN\" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="arq" uri="/tld/arquitectura.tld" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="display" uri="http://displaytag.sf.net/el" %>
<%@ taglib prefix="dynamic" uri="/tld/dynamic.tld" %>
<html>
<head>
    <jsp:include flush="true" page="Head.jsp"/>
    <dynamic:background />

    <!--Round the menu tabs-->
    <script type="text/javascript">
            window.onload = function()
            {
                Nifty("ul#nav a", "small transparent top");
                checkBrowser();
            }
    </script>

    <script type="text/javascript" xml:space="preserve">

        function showVideo()
        {
            if( ultimaFilaMarcada != "-1" )
            {
                document.location = 'video.htm?<%=VideoController.REQUEST_VIDEO_PARAM%>=' + ultimaFilaMarcada;
            }
            else
            {
                alert("You need to select a video");
            }
        }

        //Add this video to the favourite list of the user.
        //Function that execute a JSON call to the server to add this YouTube video to the personal list of the user.
        function addToMyFavorites( youTubeVideoId )
        {
           if( confirm('Add this video to your favourites?') )
           {
               var args = new Array();
               var userLogin = '<dynamic:user />';
               args[0] = "'" + youTubeVideoId + "'";
               args[1] = "'" + userLogin + "'";
               jsonEntityArrayCall( "videoFacade", "addVideo", args, cb_addToFavourites  );
           }
        }

        //Call back to the JSON call to add the video to the user favourites.
        function cb_addToFavourites( result, exception )
        {
           if( exception )
           {
               alert( exception );
           }
           else
           {
               if( result == 0 ) //OK
               {
                   alert('OK!');
               }
               else if( result == 1 ) //No user
               {
                   alert('Ooops! We don not know who are you. Are you logged in?');
               }
               else if( result == 2 ) //Video already added.
               {
                   alert('This video was already added to your favourites!');
               }
           }
        }

    </script>

</head>
<body>
<c:choose>
    <c:when test="${command.community}">
        <c:set var="currentPage" value="myCommunity" scope="request" />
    </c:when>
    <c:otherwise>
        <c:set var="currentPage" value="myVideos" scope="request" />
    </c:otherwise>
</c:choose>
<jsp:include page="Menu.jsp"/>
<center>
    <c:choose>
        <c:when test="${command.community}">
            <c:set var="videoAction" value="listVideosCommunity.htm"/>
        </c:when>
        <c:otherwise>
            <c:set var="videoAction" value="listVideosUser.htm"/>
        </c:otherwise>
    </c:choose>
    <form:form action="${videoAction}" method="post">
        <table>
            <tr align="center">
                <td align="center">Use % character as wildcard if needed</td>
            </tr>
            <tr align="center">
                <td align="center">Title:&nbsp;<form:input path="criteria.title" cssClass="inputClass"/>&nbsp;&nbsp;&nbsp;&nbsp;
                Content:&nbsp;<form:input path="criteria.content" cssClass="inputClass"/></td>
            </tr>
            <tr align="center">
                <td align="center">
                    <input type="submit" name="filterSubmit" value="Filter" class="boton_2" />
                </td>
            </tr>
        </table>
        <form:hidden path="accion"/>
        <form:hidden path="page"/>
        <br><br>
        <arq:errors visibility="global" classTable="tablaErrores" printModel="table"/>
        <display:table class="tablaDisplayTag" id="video"
                       decorator="es.onlysolutions.arq.core.mvc.list.decorator.RowIdDecorator"
                       requestURI="${videoAction}" name="command.paginatedList" export="true">
            <display:column class="displayTagCell" property="title" title="Title"/>
            <display:column class="displayTagCell" title="Image" media="html">
                <img src="<c:out value="${video.imageUrl}"/>" alt="Video" height="97" width="130"/>
            </display:column>
            <display:column class="displayTagCell" property="imageUrl" title="Image URL" media="excel pdf"/>
            <display:column class="displayTagCell" media="html" property="content" title="Content"/>
            <c:if test="${command.community}">
                <display:column class="displayTagCell" title="Add to favourites">
                    <dynamic:flashButton text="Add" id="${video.youTubeId}-${video.user.login}" link="addToMyFavorites('${video.youTubeId}')" width="150" height="30" color="red" />
                </display:column>
            </c:if>
        </display:table>
        <br><br>
        <table>
            <tr>
                <td>
                    <dynamic:flashButton text="Show" link="showVideo()" width="150" height="30" />
                </td>
            </tr>
        </table>
    </form:form>
</center>
</body>
</html>